package GUI.Administracion;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TableColumn;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.ComboVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AgregarCombo extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Table table_1;
	private Text precio;
	private Text nombre;
	Label error2;
	Label error1;
	private Text des;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarCombo(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(653, 514);
		shell.setText(getText());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 21, 206, 251);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(188);
		tblclmnNewColumn.setText("New Column");
		
		Button btnAgregarAlCombo = new Button(shell, SWT.NONE);
		btnAgregarAlCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table.getSelection()[0];
				TableItem t1 = new TableItem(table_1, SWT.NONE);
				t1.setText(0, t.getText());
				
				
			}
		});
		btnAgregarAlCombo.setBounds(222, 51, 131, 28);
		btnAgregarAlCombo.setText("Agregar Al Combo");
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		table_1.setBounds(375, 21, 189, 251);
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(186);
		tblclmnNewColumn_1.setText("New Column");
		
		Button btnQuitarDelCombo = new Button(shell, SWT.NONE);
		btnQuitarDelCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table_1.getSelection()[0];
				table_1.remove(table_1.getSelectionIndex());
			}
		});
		btnQuitarDelCombo.setBounds(222, 121, 131, 28);
		btnQuitarDelCombo.setText("Quitar del Combo");
		
		Label lblPrecioDelCombo = new Label(shell, SWT.NONE);
		lblPrecioDelCombo.setBounds(200, 302, 108, 14);
		lblPrecioDelCombo.setText("Precio del Combo");
		
		precio = new Text(shell, SWT.BORDER);
		precio.setBounds(333, 299, 64, 19);
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] faltan = table_1.getItems();
				ArticleVO [] arts= null;
				ArticleProxyMgt o = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					arts= o.getArticles();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				ArrayList<ArticleVO> lista = new ArrayList<ArticleVO>();
				for(int i=0;i<faltan.length;i++){
					for (int j=0;j<arts.length;j++){
						if(faltan[i].getText().equalsIgnoreCase(arts[j].getName())){
							lista.add(arts[j]);
							
						}
					}
					
				}
				String nom = nombre.getText(); 
				if(nom.equals("")){
					error2.setText("Ingrese el nombre del combo");
					error2.setVisible(true);
				}
				
				String prec = precio.getText();
				if(prec.equals("")){
					error1.setText("Ingrese el precio del combo");
					error1.setVisible(true);
				}
				String desc = des.getText();
				if(error2.getVisible()==false && error1.getVisible()==false){
				ComboVO combo = new ComboVO(nom,desc,Double.parseDouble(prec),lista);
			}
				
			}
		});
		btnAceptar.setBounds(441, 425, 94, 28);
		btnAceptar.setText("Aceptar");
		
		Button btnVolver = new Button(shell, SWT.NONE);
		btnVolver.setBounds(52, 425, 94, 28);
		btnVolver.setText("Volver");
		
		Label lblNombreDelCombo = new Label(shell, SWT.NONE);
		lblNombreDelCombo.setBounds(200, 334, 108, 14);
		lblNombreDelCombo.setText("Nombre del Combo");
		
		nombre = new Text(shell, SWT.BORDER);
		nombre.setBounds(333, 329, 125, 19);
		
		error1 = new Label(shell, SWT.NONE);
		error1.setBounds(418, 302, 146, 14);
		error1.setText("New Label");
		
		error2 = new Label(shell, SWT.NONE);
		error2.setBounds(464, 334, 131, 14);
		error2.setText("New Label");
		error2.setVisible(false);
		error1.setVisible(false);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(200, 373, 108, 14);
		lblNewLabel.setText("Descripcion");
		
		des = new Text(shell, SWT.BORDER);
		des.setBounds(333, 368, 248, 19);
		
		
		//Cartgo la primera tabla con los articulos 
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		ArticleVO[] arts=null;
		try {
			arts = uso.getArticles();
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		}
		for(int i=0;i<arts.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(arts[i].getName());
			
		}
		

	}
}
